package com.lucenly.spell.adapter

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.recyclerview.widget.RecyclerView
import com.lucenly.spell.R
import com.lucenly.spell.activity.SpellDetailActivity
import com.lucenly.spell.utils.DisplayUtil
import com.moregold.mvvmlib.ext.ext_doubleClick

class HomeItemAdapter:  RecyclerView.Adapter<HomeItemAdapter.ItemHolder>{
    var context: Context? = null
    var data:List<Int> = arrayListOf()
    var type : Int = 0
    fun setTypeData(type:Int){
        this.type = type
    }
    constructor(data: List<Int>, context: Context){
        this.data = data
        this.context = context
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemHolder {
        val view =
            LayoutInflater.from(parent.context).inflate(R.layout.item_spell, parent, false)
        return ItemHolder(view)
    }

    override fun getItemCount(): Int {
        return data.size
    }

    class ItemHolder(view: View) : RecyclerView.ViewHolder(view) {
        val contentView = view.findViewById<LinearLayout>(R.id.itemView)
        val view = view.findViewById<View>(R.id.view)
        val llGoSpell = view.findViewById<LinearLayout>(R.id.llGoSpell)
    }

    override fun onBindViewHolder(holder: ItemHolder, position: Int) {
        if (type==0){
            holder.view.visibility = View.GONE
            holder.contentView.setBackgroundColor(Color.parseColor("#ffffff"))
        }else{
            holder.view.visibility = View.VISIBLE
            holder.contentView.setBackgroundResource(R.drawable.spell_item_shape)
        }
        holder.llGoSpell.ext_doubleClick {
            context?.let {
                it.startActivity(Intent(it,SpellDetailActivity::class.java))
            }
        }
    }
}